package com.dongdongshop.aop;

import com.alibaba.fastjson.JSONObject;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LoggerAspect {

    private final Logger logger = LoggerFactory.getLogger(LoggerAspect.class);

    //定义切点表达式
    @Pointcut("execution(public * com.dongdongshop.service..*.*(..))")
    public void aopLogger(){}

    //定义前置通知
    @Before("aopLogger()")
    public void doBefore(JoinPoint joinpoint) throws  Throwable{
        //获取参数
        Object[] args = joinpoint.getArgs();
        //获取切点（正在被代理的方法）
        Signature signature = joinpoint.getSignature();

        logger.info("进入方法：{}，参数为：{}",signature,args);
    }

    //后置通知
    @AfterReturning(value = "aopLogger()",returning = "result")
    public void doAfter(Object result) throws Throwable{
        //处理请求成功后，打印返回的结果
        logger.info("方法执行成功，返回值为：{}", JSONObject.toJSONString(result));
    }
}
